package com.renli.controller;

import com.renli.bean.ResultJson;
import com.renli.pojo.*;
import com.renli.service.PersonnelManagementService;
import com.renli.vo.ArchivesVo;
import com.renli.vo.SelectArchives;
import com.renli.vo.SelectChangeStatisticsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: zdk
 * Date: 2020/09/09 14:59
 * Description:
 * Version: V1.0
 */
@Controller
@Api(value = "档案管理接口", tags = "档案管理接口API")
@ResponseBody
public class PersonnelManagementController {
    @Autowired
    private PersonnelManagementService personnelManagementService;

    @RequestMapping("/PM/query")

    public List<Archives> query(int id, int state, int state1, String createTime) {
        System.out.println(id);
        System.out.println("pwmmm");
        ResultJson resultJson = new ResultJson();
        List<Archives> list = personnelManagementService.query_person(id, state, state1, createTime);
        if (list.size() > 0) {
            return null;
        }
        return null;
    }
    @RequestMapping(value = "/selectResumeNotUse", method = RequestMethod.POST)
    @ApiOperation(value = "遍历出已录取但并未创建档案的简历信息", notes = "遍历出已录取但并未创建档案的简历信息供客户选择并绑定档案", httpMethod = "post", response = ResultJson.class)
    public ResultJson selectResumeNotUse() {
        return personnelManagementService.selectResumeNotUse();
    }

    @RequestMapping(value = "/insertArchives", method = RequestMethod.POST)
    @ApiOperation(value = "添加档案接口(直接提交审核)", notes = "传职位发布数据进行添加审核", httpMethod = "post", response = ResultJson.class)
    public ResultJson insertArchives( @ApiParam(name = "archives", value = "档案详细信息(表单数据)", required = true) BackupArhives backupArhives,
                                         @ApiParam(name = "changeStatistics", value = "当前USER信息(表单数据)", required = false) ChangeStatistics changeStatistics,
                                        @ApiParam(name = "jpg", value = "头像文件", required = false) MultipartFile file,
                                        HttpServletRequest request) {
        if (file==null){return personnelManagementService.insertArchives(backupArhives,changeStatistics);}

        else {
            ResultJson resultJson=new ResultJson();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
            String res = sdf.format(new Date());
            String name=backupArhives.getBackrealName();
            // uploads文件夹位置
            String rootPath = request.getSession().getServletContext().getRealPath("head/");
            // 原始名称
            String originalFileName = file.getOriginalFilename();
            String substring = originalFileName.substring(originalFileName.lastIndexOf("."));//后缀名
            if (substring.equals(".jpg")||substring.equals(".png")){
                // 新文件名
                String newFileName = name + res + substring;
                // 创建年月文件夹
                Calendar date = Calendar.getInstance();
                File dateDirs = new File(date.get(Calendar.YEAR) + File.separator + (date.get(Calendar.MONTH) + 1));

                // 新文件
                File newFile = new File(rootPath + File.separator + dateDirs + File.separator + newFileName);
                // 判断目标文件所在目录是否存在
                if (!newFile.getParentFile().exists()) {
                    // 如果目标文件所在的目录不存在，则创建父目录
                    newFile.getParentFile().mkdirs();
                }


                // 将内存中的数据写入磁盘
                try {
                    file.transferTo(newFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }


                backupArhives.setBackphoto( "download"+ File.separator + dateDirs + File.separator + newFileName);
                System.out.println( "download"+ File.separator + dateDirs + File.separator + newFileName);
                return personnelManagementService.insertArchives(backupArhives,changeStatistics);
            }
            else {
                return resultJson.setFail("添加失败,头像文件格式错误");
            }}

    }

    @RequestMapping(value = "/insertArchivesSave", method = RequestMethod.POST)
    @ApiOperation(value = "添加档案接口（保存）", notes = "传职位发布数据进行保存（待审核）", httpMethod = "post", response = ResultJson.class)
    public ResultJson insertArchivesSave( @ApiParam(name = "archives", value = "档案详细信息(表单数据)", required = true) BackupArhives backupArhives,
                                      @ApiParam(name = "changeStatistics", value = "当前USER信息(表单数据)", required = false) ChangeStatistics changeStatistics,
                                      @ApiParam(name = "jpg", value = "头像文件", required = false) MultipartFile file,
                                      HttpServletRequest request) {
        if (file==null){return personnelManagementService.insertArchivesSave(backupArhives,changeStatistics);}

        else {
            ResultJson resultJson=new ResultJson();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
            String res = sdf.format(new Date());
            String name=backupArhives.getBackrealName();
            // uploads文件夹位置
            String rootPath = request.getSession().getServletContext().getRealPath("head/");
            // 原始名称
            String originalFileName = file.getOriginalFilename();
            String substring = originalFileName.substring(originalFileName.lastIndexOf("."));//后缀名
            if (substring.equals(".jpg")||substring.equals(".png")){
                // 新文件名
                String newFileName = name + res + substring;
                // 创建年月文件夹
                Calendar date = Calendar.getInstance();
                File dateDirs = new File(date.get(Calendar.YEAR) + File.separator + (date.get(Calendar.MONTH) + 1));

                // 新文件
                File newFile = new File(rootPath + File.separator + dateDirs + File.separator + newFileName);
                // 判断目标文件所在目录是否存在
                if (!newFile.getParentFile().exists()) {
                    // 如果目标文件所在的目录不存在，则创建父目录
                    newFile.getParentFile().mkdirs();
                }


                // 将内存中的数据写入磁盘
                try {
                    file.transferTo(newFile);
                } catch (IOException e) {
                    e.printStackTrace();
                }


                backupArhives.setBackphoto( "download"+ File.separator + dateDirs + File.separator + newFileName);
                System.out.println( "download"+ File.separator + dateDirs + File.separator + newFileName);
                return personnelManagementService.insertArchivesSave(backupArhives,changeStatistics);
            }
            else {
                return resultJson.setFail("添加失败,头像文件格式错误");
            }}

    }

    @RequestMapping(value = "/SubmitForReview", method = RequestMethod.POST)
    @ApiOperation(value = "新增档案提交审核", notes = "提交审核", httpMethod = "post", response = ResultJson.class)
    public ResultJson selectArchives(@RequestBody @ApiParam(name = "archives", value = "changeStatistics（changeStatisticsId） BackupArhives(realName,backupArhivesId)", required = true) Map map) {
        return personnelManagementService.SubmitForReview(map);
    }

    @RequestMapping(value = "/insertArchivesReview", method = RequestMethod.POST)
    @ApiOperation(value = "（添加）档案审核", notes = "（添加）档案审核", httpMethod = "post", response = ResultJson.class)
    public ResultJson insertArchivesReview( @RequestBody @ApiParam(name = "event", value = "userId(当前复核user) state 1通过 -1不通过", required = true)Event event){
                    return personnelManagementService.insertArchivesReview(event);
    }

    @RequestMapping(value = "/delectArchives", method = RequestMethod.POST)
    @ApiOperation(value = "删除档案接口（提交审核前）", notes = "传需要删除的档案ID", httpMethod = "post", response = ResultJson.class)
    public ResultJson delectArchives(@RequestBody @ApiParam(name = "archives", value = "档案详细信息(只需要ID)", required = true) Archives archives) {
        return personnelManagementService.delectArchives(archives);
    }

    @RequestMapping(value = "/selectArchives", method = RequestMethod.POST)
    @ApiOperation(value = "查询档案接口", notes = "查询档案（跟据条件查询） 单个查询", httpMethod = "post", response = ResultJson.class)
    public ResultJson selectArchives(@RequestBody @ApiParam(name = "查询条件", value = "查询条件", required = true)SelectArchives selectArchives) {
        return personnelManagementService.selectArchives(selectArchives);
    }


    @RequestMapping(value = "/selectchangeType", method = RequestMethod.POST)
    @ApiOperation(value = "遍历变更类型", notes = "遍历档案的变更类型进行选择", httpMethod = "post", response = ResultJson.class)
    public ResultJson selectchangeType() {
        return personnelManagementService.selectchangeType();
    }

    @RequestMapping(value = "/alternationsArchivesAudit", method = RequestMethod.POST)
    @ApiOperation(value = "档案变更审核（点击审核,无保存功能）", notes = "档案变更审核(点击审核)", httpMethod = "post", response = ResultJson.class)
    public ResultJson alternationsArchivesAudit( @ApiParam(name = "变更前档案信息", value = "变更前档案信息(只传ID)", required = true)Archives archives,
                                        @ApiParam(name = "变更后档案信息", value = "变更后档案信息(表单数据（变更的数据）)", required = true)BackupArhives backupArhives,
                                        @ApiParam(name = "变更类型", value = "变更类型", required = true)ChangeType changeType,
                                        @ApiParam(name = "当前user", value = "当前user", required = true)User user,
                                        @ApiParam(name = "jpg", value = "头像文件", required = false) MultipartFile file,
                                        HttpServletRequest request) {
        if (changeType.getId()!=6){
            return personnelManagementService.alternationsArchivesAudit(archives,backupArhives,changeType,user);
        }else {
            if (file==null){ return personnelManagementService.alternationsArchivesAudit(archives,backupArhives,changeType,user);}

            else {
                ResultJson resultJson=new ResultJson();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSS");
                String res = sdf.format(new Date());
                String name=archives.getRealName();
                // uploads文件夹位置
                String rootPath = request.getSession().getServletContext().getRealPath("head/");
                // 原始名称
                String originalFileName = file.getOriginalFilename();
                String substring = originalFileName.substring(originalFileName.lastIndexOf("."));//后缀名
                if (substring.equals(".jpg")||substring.equals(".png")){
                    // 新文件名
                    String newFileName = name + res + substring;
                    // 创建年月文件夹
                    Calendar date = Calendar.getInstance();
                    File dateDirs = new File(date.get(Calendar.YEAR) + File.separator + (date.get(Calendar.MONTH) + 1));

                    // 新文件
                    File newFile = new File(rootPath + File.separator + dateDirs + File.separator + newFileName);
                    // 判断目标文件所在目录是否存在
                    if (!newFile.getParentFile().exists()) {
                        // 如果目标文件所在的目录不存在，则创建父目录
                        newFile.getParentFile().mkdirs();
                    }


                    // 将内存中的数据写入磁盘
                    try {
                        file.transferTo(newFile);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }


                    archives.setPhoto( "download"+ File.separator + dateDirs + File.separator + newFileName);
                    System.out.println( "download"+ File.separator + dateDirs + File.separator + newFileName);
                    return personnelManagementService.alternationsArchivesAudit(archives,backupArhives,changeType,user);
                }
                else {
                    return resultJson.setFail("失败,头像文件格式错误");
                }}
        }

    }

    @RequestMapping(value = "/selectChangeStatistics", method = RequestMethod.POST)
    @ApiOperation(value = "遍历档案变更信息", notes = "查询档案变更（跟据条件查询） 单个查询", httpMethod = "post", response = ResultJson.class)
    public ResultJson selectArchives(@RequestBody @ApiParam(name = "查询条件", value = "查询条件", required = true)SelectChangeStatisticsVo selectChangeStatisticsVo) {
        return personnelManagementService.selectChangeStatistics(selectChangeStatisticsVo);
    }

    @RequestMapping(value = "/updateChangeStatisticsReview", method = RequestMethod.POST)
    @ApiOperation(value = "（变更）档案审核", notes = "（变更）档案审核", httpMethod = "post", response = ResultJson.class)
    public ResultJson selectArchives(@RequestBody @ApiParam(name = "ChangeStatistics", value = "变更前档案id 变更档案id（backId）eventId", required = true)ChangeStatistics changeStatistics,
                                     @RequestBody @ApiParam(name = "event", value = "当前user,state(-1不同意，1同意)", required = true) Event event) {
        return personnelManagementService.updateChangeStatisticsReview(changeStatistics,event);
    }
}